﻿using System;
using System.Collections;
using System.Collections.Generic;

using BizElements.BusinessLayer;

namespace BizElements.Web
{
    /// <summary>
    /// Collection of <see cref="IBindableField"/> objects.
    /// </summary>
    [Serializable]
    public sealed class BindableFieldCollection : IBindableFieldCollection
    {
        #region FieldPermissions.

        List<IBindableField> list = new List<IBindableField>();

        #endregion

        #region Properties and indexers.

        /// <summary>
        /// Searches the collection for the field with the specified <see cref="IBindableField.BindingName"/>.
        /// </summary>
        /// <param name="bindingName">Name of the bound property.</param>
        /// <returns>Specifies field or <b>null</b> if the field with the given name is not found in the collection.</returns>
        public IBindableField this[string bindingName]
        {
            get
            {
                foreach (IBindableField field in this.list)
                    if (field.BindingName == bindingName)
                        return field;

                return null;
            }
        }

        /// <summary>
        /// Gets the element at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index of the element to get or set. </param>
        /// <returns>The element at the specified index.</returns>
        public IBindableField this[int index]
        {
            get { return this.list[index]; }
        }

        /// <summary>
        /// Gets the number of elements actually contained in the collection.
        /// </summary>
        public int Count
        {
            get { return this.list.Count; }
        }

        #endregion

        #region Add.

        /// <summary>
        /// Adds a <b>IBindableField</b> to the end of the <b>BindableFieldCollection</b>.
        /// </summary>
        /// <param name="field">The field to be added to the end of the collection. <b>null</b> is not allowed.</param>
        /// <returns>The field which was added to <b>BindableFieldCollection</b>.</returns>
        public IBindableField Add(IBindableField field)
        {
            if (field == null)
                throw new ArgumentException("Null is not allowed.");

            this.list.Add(field);
            return field;
        }

        /// <summary>
        /// Creates and adds a <b>IBindableField</b> to the end of the <b>BindableFieldCollection</b>.
        /// </summary>
        /// <param name="bindingName">Name of the property that is displayed and modified in binding engine. Null is <b>not</b> allowed.</param>
        /// <param name="caption">Caption/description of the bound field. <b>Nullable</b>.</param>
        /// <param name="dataType">Type of the value stored in the property. Null is <b>not</b> allowed. 
        /// Primitive .NET types should be used whenever possible.</param>
        /// <returns>The field which was added to <b>BindableFieldCollection</b>.</returns>
        public IBindableField Add(string bindingName, string caption, Type dataType)
        {
            IBindableField newField = new BindableField(bindingName, caption, dataType);
            return Add(newField);
        }

        #endregion

        #region IEnumerable.

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.list.GetEnumerator();
        }

        #endregion
    }
}